// by Xeno
#define __DEBUG__
#include "x_setup.sqf"
#define THIS_FILE "fn_createnexttarget.sqf"
private ["_current_target_pos","_current_target_radius","_dummy","_emptyH","_current_target_name"];
if (!isServer) exitWith{};

sleep _this;

__TRACE("start");

[QGVAR(current_target_index),GVAR(maintargets_list) select GVAR(current_counter)] call FUNC(NetSetJIP);
__INC(GVAR(current_counter));

sleep 1.0123;
if (GVAR(first_time_after_start)) then {
	GVAR(first_time_after_start) = false;
	__TRACE("First Start pause")
	sleep 25.123;
};

GVAR(update_target) = false;
GVAR(main_target_ready) = false;
GVAR(side_main_done) = false;
GVAR(sum_camps) = -91;
GVAR(mines_created) = [];
// TYRGHEN - Priority Targets
GVAR(priority_objects) = [];

_dummy = GVAR(target_names) select __XJIPGetVar(GVAR(current_target_index));
_current_target_pos = _dummy select 0;
_current_target_name = _dummy select 1;
_current_target_radius = _dummy select 2;

__TRACE_1("","_dummy");

GVAR(target_radius) = _current_target_radius;
GVAR(mttarget_radius_patrol) = _current_target_radius + 200 + random 200;

_tname = _current_target_name call FUNC(KBUseName);
#ifndef __TT__
_tsar = if (GVAR(WithLessArmor) == 0) then {
	["('Man' countType thislist >= d_man_count_for_target_clear) && {('Tank' countType thislist >= d_tank_count_for_target_clear)} && {('Car' countType thislist  >= d_car_count_for_target_clear)}", "X_JIPH setVariable ['target_clear',false,true];d_update_target=true;['d_update_target'] call d_fnc_NetCallEventToClients;d_kb_logic1 kbTell [d_kb_logic2,d_kb_topic_side,'Attack',['1','',""" + _current_target_name + """,[""" + _tname + """]],'SIDE'];deleteVehicle d_check_trigger", ""]
} else {
	["('Man' countType thislist >= d_man_count_for_target_clear)", "X_JIPH setVariable ['target_clear',false,true];d_update_target=true;['d_update_target'] call d_fnc_NetCallEventToClients;d_kb_logic1 kbTell [d_kb_logic2,d_kb_topic_side,'Attack',['1','',""" + _current_target_name + """,[""" + _tname + """]],'SIDE'];deleteVehicle d_check_trigger;", ""]
};
#else
_tsar = if (GVAR(WithLessArmor) == 0) then {
	["('Man' countType thislist >= d_man_count_for_target_clear) && {('Tank' countType thislist >= d_tank_count_for_target_clear)} && {('Car' countType thislist  >= d_car_count_for_target_clear)}", "X_JIPH setVariable ['target_clear',false,true];d_update_target=true;['d_update_target'] call d_fnc_NetCallEventToClients;d_hq_logic_en1 kbTell [d_hq_logic_en2,'HQ_W','Attack',['1','',""" + _current_target_name + """,[""" + _tname + """]],'SIDE'];d_hq_logic_ru1 kbTell [d_hq_logic_ru2,'HQ_E','Attack',['1','',""" + _current_target_name + """,[""" + _tname + """]],'SIDE'];deleteVehicle d_check_trigger", ""]
} else {
	["('Man' countType thislist >= d_man_count_for_target_clear)", "X_JIPH setVariable ['target_clear',false,true];d_update_target=true;['d_update_target'] call d_fnc_NetCallEventToClients;d_hq_logic_en1 kbTell [d_hq_logic_en2,'HQ_W','Attack',['1','',""" + _current_target_name + """,[""" + _tname + """]],'SIDE'];d_hq_logic_ru1 kbTell [d_hq_logic_ru2,'HQ_E','Attack',['1','',""" + _current_target_name + """,[""" + _tname + """]],'SIDE'];deleteVehicle d_check_trigger", ""]
};
#endif

GVAR(check_trigger) = [_current_target_pos, [_current_target_radius + 20, _current_target_radius + 20, 0, false], [GVAR(enemy_side_trigger), "PRESENT", false], _tsar] call FUNC(CreateTrigger);

[_current_target_pos, _current_target_radius] spawn FUNC(docreatenexttarget);

while {!GVAR(update_target)} do {sleep 2.123};

#ifdef __TT__
_tsar = ["(X_JIPH getVariable 'd_mt_radio_down') && {d_side_main_done} && {((X_JIPH getVariable 'd_campscaptured_e') == d_sum_camps || {(X_JIPH getVariable 'd_campscaptured_w') == d_sum_camps})} && {('Car' countType thislist <= d_car_count_for_target_clear)} && {('Tank' countType thislist <= d_tank_count_for_target_clear)} && {('Man' countType thislist <= d_man_count_for_target_clear)}", "0 = 0 spawn d_fnc_target_clear", ""];
#else
_tsar = ["(X_JIPH getVariable 'd_mt_radio_down') && {d_side_main_done} && {(X_JIPH getVariable 'd_campscaptured') == d_sum_camps} && {('Car' countType thislist <= d_car_count_for_target_clear)} && {('Tank' countType thislist <= d_tank_count_for_target_clear)} && {('Man' countType thislist <= d_man_count_for_target_clear)}", "0 = 0 spawn d_fnc_target_clear", ""];
#endif

GVAR(current_trigger) = [_current_target_pos, [_current_target_radius  + 50, _current_target_radius + 50, 0, false],[GVAR(enemy_side_trigger), "PRESENT", false], _tsar] call FUNC(CreateTrigger);
if (!isNil QGVAR(HC_CLIENT_OBJ)) then {
	[QGVAR(mct), [GVAR(HC_CLIENT_OBJ), [_current_target_pos, [_current_target_radius  + 50, _current_target_radius + 50, 0, false],[GVAR(enemy_side_trigger), "PRESENT", false], ["this","",""]]]] call FUNC(NetCallEventSTO);
};
